package ldh.im.fxbase.dao;

import ldh.im.fxbase.pojo.DbMessage;
import ldh.im.fxbase.pojo.ext.MessageStatus;
import ldh.im.fxbase.util.DbUtil;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.lang3.StringEscapeUtils;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class MessageDao extends BaseDao {

    private static final String TABLE_NAME = "message";

    public List<DbMessage> getMessagesByImGroupId(Long imGroupId, int size) throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select * from message where groupId = ? order by createTime desc limit 0, %s", size);
            List<DbMessage> list = queryRunner.query(connection, sql, new BeanListHandler<DbMessage>(DbMessage.class), imGroupId);
            list.forEach(m-> {
                m.setContent(StringEscapeUtils.unescapeHtml4(m.getContent()));
                m.initMessageExt();
            });
            Collections.reverse(list);
            return list;
        });
    }

    public void saveMessage(DbMessage message) throws SQLException {
        DbUtil.transaction((queryRunner, connection) -> {
            ResultSetHandler<Long> h = new ScalarHandler();
            String sql = String.format("insert into message(serverId, messageType, content, groupId, ext, status, createTime, updateTime) values(?, ?, ?, ?, ?, ?, ?, ?)");
            Long n = queryRunner.insert(connection, sql, h, message.getServerId(), message.getMessageType().name(), StringEscapeUtils.escapeHtml4(message.getContent())
                    , message.getImGroup().getId(), message.getExt(), message.getStatus().name(), message.getCreateDateTime(), message.getCreateDateTime());
            message.setId(n);
            return n;
        });
    }

    public void saveMessages(List<DbMessage> messages) throws SQLException {
        for(DbMessage dbMessage : messages) {
            saveMessage(dbMessage);
        }
    }

    public void changeMessageStatus(Long messageId, MessageStatus messageStatus) throws SQLException {
        DbUtil.transaction((queryRunner, connection) -> {
            DbMessage message = getBeanById(queryRunner, connection, DbMessage.class, TABLE_NAME, messageId);

            String sql = String.format("update message set status = ?, updateTime=?, version=？ where id = ? and version = ?");
            updateBeanById(queryRunner, connection, sql, messageStatus.name(), new Date(),
                    messageId, message.getVersion()+1, messageId, message.getVersion());
            return null;
        });
    }

    public void changeMessageExt(Long messageId, String ext) throws SQLException {
        DbUtil.transaction((queryRunner, connection) -> {
            DbMessage message = getBeanById(queryRunner, connection, DbMessage.class, TABLE_NAME, messageId);

            String sql = String.format("update message set ext = ?, updateTime=?, version=？ where id = ? and version = ?");
            updateBeanById(queryRunner, connection, sql, ext, new Date(),
                    messageId, message.getVersion()+1, messageId, message.getVersion());
            return null;
        });
    }

}
